<?php
/**
 * Created by PhpStorm.
 * User: 36980
 * Date: 2017/09/06
 * Time: 15:07
 */

namespace Admin\Controller;

use Think\Controller;


class ArticlesController extends Controller
{
    /***
     * 展示
     */
    public function index()
    {
        //$spare1属于类型的字段. 0是专题 1 是广告 根据KEY进行查询
        $id = trim(I('post.key'));
        if (!empty($id)) {
            $spare1 = 0;
            ArticlesController::selectById($id, $spare1);
        } else {
            $spare1 = 0;
            ArticlesController::select($spare1);
        }

        $this->display();
    }

    /**
     * 查询
     */
    public function select($spare1)
    {
        $articles = M('Articles');
        $count = $articles->where("spare1 = $spare1")->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $member = $articles->limit($Page->firstRow . ',' . $Page->listRows)->where("spare1 = $spare1")->order()->select();
        $this->assign('articles', $member);
        $this->assign('page', $show);


    }

    /**
     * 根据id查询
     */
    public function selectById($id, $spare1)
    {
        //实例化model
        $articles = M('articles');
        //根据modle查询
        $articles = $articles->where("arName = $id and spare1=$spare1")->select();
        $this->assign('articles', $articles);

    }

    /***
     * 增加
     */
    public function insert()
    {
        //不是post 请求返回数据
        if (!IS_POST) {
            $articles = M('articles')->where()->select();
            $this->articles = $articles;
            $this->display();
        }
        if (IS_POST) {
            //上传图片 调用上传的方法
            if (!empty($_FILES['fileToUpload']['name'])) {
                $_POST['arImgs'] = ArticlesController::upload($_FILES['fileToUpload']);
            }
            //判断名称的大小和是否为空值
            if (strlen($_POST['arName']) > 12 || empty($_POST['arName'])) {
                $this->error('名称太长或输没输入内容，请从新输入！');
            }
            //判断时间因为是两个时间 开始和结束 如果开始大于结束 结构上不正确
            if (strtotime($_POST['startTime'] >= $_POST['endTime'])) {
                $this->error('开始时间不能大于或等于结束时间，请从新输入！');
            }
            //两个地址必须要输入一个 不进行判断地址内容是否正确.
            if (empty($_POST['arIOS']) && empty($_POST['arAandroid'])) {
                $this->error('地址起码要输入一个，请从新输入！');
            }
//            $hot = M('articles')->where('ishot = 1')->count();
//            if($hot>=4){
//                $this->error('当前时段的热门推荐只能有四个');
//            }

            $data['arName'] = $_POST['arName']; //名称
            if ($_POST['arImgs']) {
                $data['arImgs'] = $_POST['arImgs']; //图片
            }
            $data['arIOS'] = $_POST['arIOS']; //地址
            $data['arAandroid'] = $_POST['arAandroid']; //地址
            $data['startTime'] = $_POST['startTime'];//开始时间
            $data['endTime'] = $_POST['endTime'];//结束时间
            $data['remark'] = $_POST['remark'];//备注
            $data['ishot'] = $_POST['ishot']; //推荐
            $data['spare1'] = 0;
            $data['addTime'] = date("Y-m-d H:i:s");
            $articles = M('Articles');
            if (!$articles->create()) {
                $this->error($articles->getError());
            } else {
                if ($articles->data($data)->add()) {
                    $this->success("添加成功", U('Articles/index'));
                } else {
                    $this->error("添加失败");
                }

            }

        }

    }

    /***
     * 修改
     */
    public function update()
    {
        if (!IS_POST) {

            $id = I('id');
            $articles = M('articles')->where("arId = $id")->find();
            $this->assign('post', $articles);
            $this->display();
        }
        if (IS_POST) {
            if (!empty($_FILES['fileToUpload']['name'])) {
                $_POST['arImgs'] = ArticlesController::upload($_FILES['fileToUpload']);
            }
            //接收参数
            if (strlen($_POST['arName']) > 12 || empty($_POST['arName'])) {
                $this->error('名称太长或输没输入内容，请从新输入！');
            }

            if (strtotime($_POST['startTime'] >= $_POST['endTime'])) {
                $this->error('开始时间不能大于或等于结束时间，请从新输入！');
            }
            if (empty($_POST['arIOS']) && empty($_POST['arAndroid'])) {
                $this->error('地址起码要输入一个，请从新输入！');
            }
            $articles = M('articles');
            $id = $_POST['arId'];
            $data['arName'] = $_POST['arName'];
            $data['arIOS'] = $_POST['arIOS'];
            $data['arAndroid'] = $_POST['arAndroid'];
            //判断图片是否有上传图片 如果有上传就先做查询找出图片原来路径储存
            if ($_POST['arImgs']) {
                $data['arImgs'] = $_POST['arImgs']; //图片

                //查询图片
                $list = $articles->where("arId = $id")->field('arImgs')->find();
            }
            $data['spare1'] = 0;
            $data['startTime'] = $_POST['startTime'];
            $data['endTime'] = $_POST['endTime'];
            $data['remark'] = $_POST['remark'];
            if(empty($_POST['ishot'])){
                $data['ishot'] = 0;
            }else{
                $data['ishot']  = $_POST['ishot'];
            }

            $data['upnum'] = $_POST['upnum'];

            if ($articles->where("arId=$id")->data($data)->save() !== false) {
                if ($_POST['arImgs']) {
                    //删除原来图片 节省服务内存空间
                    unlink('./Public/icon/' . $list['arimgs']);
                }

                $this->success("更新成功", U('Articles/index'));
            } else {
                $this->error("更新失败或填入数据没有变动");
            }
            //  unlink(“uppic/”.$img);
        }
    }

    /**
     * 删除
     */
    public function delete()
    {
        $id = I('id');
        $articles = M('articles');
        $list = $articles->where("arId = $id")->field('arImgs')->find();
        $model = $articles->where("arId = $id")->delete();
        if ($model) {
            //删除成功后删除图片
            unlink('./Public/icon/' . $list['arimgs']);
            $this->success("删除成功", U('Articles/index'));
        } else {
            $this->error("删除失败");
        }
    }

    /**
     * 上传封装
     * @param $files 上传文件
     * @return string 文件路径
     */
    public function upload($files)
    {

        $upload = new \Think\Upload();// 实例化上传类
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/icon/'; // 设置附件上传根目录
        // 上传单个文件
        $info = $upload->uploadOne($files);
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功 获取上传文件信息

            header('Content-type:text/html;charset=utf-8');

            return $image_file = $info['savepath'] . $info['savename'];
            //  $_POST['arImgs'] = $image_file;
        }

    }

    /**
     * 广告查询
     */
    public function advertisement()
    {
        $id = trim(I('post.key'));
        if (!empty($id)) {
            $spare1 = 1;
            ArticlesController::selectById($id, $spare1);
        } else {
            $spare1 = 1;
            ArticlesController::select($spare1);
        }

        $this->display();
    }

    /**
     * 广告增加
     */
    public function insertad()
    {
        if (!IS_POST) {
            $articles = M('articles')->where()->select();
            $this->articles = $articles;
            $this->display();
        }
        if (IS_POST) {
            if (empty($_POST['listid'])) {
                $this->error('ID不能为空');
            }
            if (strtotime($_POST['startTime'] >= $_POST['endTime'])) {
                $this->error('开始时间不能大于或等于结束时间，请从新输入！');
            }
            $id = $_POST['listid'];
            $list = M('list')->where("id = $id")->find();
            // $hebin = M('Hebing')->where("appid = $id")->find();
            $data['arName'] = $list['nickname'];
            if ($list['ext'] == 'ipa') {
                $data['arIOS'] = $_SERVER['HTTP_HOST'] . __ROOT__  ."/a/". $list['shorturl'];
            } else {
                $data['arAndroid'] = $_SERVER['HTTP_HOST'] . __ROOT__ ."/a/". $list['shorturl'];
            }
            $data['arImgs'] = $list['zxlogo'];
            $data['spare2'] = $list['hslogo'];
            $data['spare1'] = 1;
            $data['startTime'] = $_POST['startTime'];//开始时间
            $data['endTime'] = $_POST['endTime'];//结束时间
            if(empty($_POST['remark'])){
                $data['remark'] = $list['beifen']; //备注
            }else{
                $data['remark'] = $_POST['remark']; //备注
            }


            $articles = M('articles');
            if ($articles->data($data)->add()) {
                $this->success("添加成功", U('Articles/advertisement'));
            } else {
                $this->error("添加失败");
            }
        }
    }

    /**
     * 广告修改
     *
     */
    public function updatead()
    {
        if (!IS_POST) {

            $id = I('id');
            $articles = M('articles')->where("arId = $id")->find();
            if(!empty($articles['arios'])){
            $web= strstr($articles['arios'],"/a/");
            }else{
                $web= strstr($articles['arandroid'],"/a/");
            }
            $web= substr ($web,3);
            $list = M('list')->where("shorturl = '$web' ")->find();
            $this->assign('post', $articles);
            $this->assign('list', $list);
            $this->display();
        }
        if(IS_POST){
           $id = $_POST['arId'];
           $listid = $_POST['id'];

            if (strtotime($_POST['startTime'] >= $_POST['endTime'])) {
                $this->error('开始时间不能大于或等于结束时间，请从新输入！');
            }

            $list = M('list')->where("id = $listid")->find();
            // $hebin = M('Hebing')->where("appid = $id")->find();
            $data['arName'] = $list['nickname'];
            if ($list['ext'] == 'ipa') {
                $data['arIOS'] = $_SERVER['HTTP_HOST'] . __ROOT__  ."/a/". $list['shorturl'];
            } else {
                $data['arAndroid'] = $_SERVER['HTTP_HOST'] . __ROOT__ ."/a/". $list['shorturl'];
            }
            $data['arImgs'] = $list['zxlogo'];
            $data['spare2'] = $list['hslogo']; //64位图片地址
            $data['spare1'] = 1;
            $data['startTime'] = $_POST['startTime'];//开始时间
            $data['endTime'] = $_POST['endTime'];//结束时间
            $data['remark'] = $_POST['remark'];
            $articles = M('articles');
            if ($articles->where("arId=$id")->data($data)->save() !== false) {
                $this->success("修改成功", U('Articles/advertisement'));
            }else{
                $this->error("修改失败");
            }
        }
    }

    public  function deletead(){
        $id = I('id');
        $articles = M('articles');

        $model = $articles->where("arId = $id")->delete();
        if ($model) {
            $this->success("删除成功", U('Articles/advertisement'));
        } else {
            $this->error("删除失败");
        }
    }
}